package com.gd.deron.smartrobot.baobao.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChineseUtil {


/**
  * 判断中文是否是乱码
  *
  * @param strName 字符串
  * @return 是否是乱码
  */
 public static boolean isMessyCode(String strName) {  
     //Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
     String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{} 【】‘； ：”“’。，、？]"; // 清除掉所有特殊字符 
     // Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
     Pattern p = Pattern.compile(regEx); 
     Matcher m = p.matcher(strName);  
     String after = m.replaceAll("");  
     String temp = after.replaceAll("\\p{P}", "");  
     char[] ch = temp.trim().toCharArray();  
     float chLength = 0 ;  
     float count = 0;  
     for (int i = 0; i < ch.length; i++) {  
         char c = ch[i];  
         if (!Character.isLetterOrDigit(c)) {  
             if (!isChinese(c)) {  
                 count = count + 1;  
             }  
             chLength++;   
         }  
     }  
     float result = count / chLength ;  
     if (result > 0.4) {  
         return true;  
     } else {  
         return false;  
     }  
 }  

	/**
	 * 
	 * 判定输入汉字
	 * @param c
	 * 
	 * @return
	 * 
	 */

	public static boolean isChinese(char c) {

		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

			return true;

		}
		return false;
	}

	/**
	 * 
	 * 检测String是否全是中文
	 * @param name
	 * 
	 * @return
	 * 
	 */

	public static boolean checkNameChese(String name) {

		boolean res = true;
		char[] cTemp = name.toCharArray();
		for (int i = 0; i < name.length(); i++) {

			if (!isChinese(cTemp[i])) {
				res = false;
				break;
			}
		}
		return res;

	}
}
